package com.zretc.blog.service;

import com.zretc.api.pojo.Blog;

import com.zretc.api.pojo.File;
import com.zretc.api.pojo.Pager;
import com.zretc.blog.dao.BlogMapper;
import com.zretc.common.constant.WebConstant;
import com.zretc.common.entity.R;
import com.zretc.common.entity.RespResult;
import com.zretc.redis.service.RedisService;
import com.zretc.security.entity.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class BlogService {
    @Autowired
    private FileFeignClient fileFeignClient;
    @Autowired
    private PointFeignClient pointFeignClient;
    @Resource
    private  BlogMapper blogMapper;
    @Autowired
    RedisService redisService;



    public RespResult addBlog(Blog blog,Integer userId){

        //增加博文操作
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();
blog.setBlogPublisher(user.getUser().getUsername());
        if(Objects.isNull(blog.getBlogTitle())) {
            return R.fail(WebConstant.BLOG_ERROR_CODE, WebConstant.ERROR_300);
        }
        else{

            blogMapper.insert(blog);
            redisService.deleteObject("blog:" + blog.getBlogPublisher());

            return pointFeignClient.addBlog(userId);
        }

    }
    public RespResult deleteBlog(Blog blog){
        if(Objects.isNull(blog.getBlogId())) {
            return R.fail(WebConstant.BLOG_ERROR_CODE, WebConstant.ERROR_404);
        }
        else{
            blogMapper.deleteBlog(blog);
            redisService.deleteObject("blog:" + blog.getBlogPublisher());
            return R.success("删除成功");
        }
    }
    public RespResult updateBlog(Blog blog){
        //修改博文操作
        if(Objects.isNull(blog.getBlogId())) {
            return R.fail(WebConstant.BLOG_ERROR_CODE, WebConstant.ERROR_404);
        }
        else{
            blogMapper.updateBlogByBlogId(blog);
            redisService.deleteObject("blog:" + blog.getBlogPublisher());
            return R.success("修改成功");
        }

    }


 public Map<String,Object> queryBlogByBlogTitle(String blogTitle,Integer pageNum,Integer pageSize) {
     Pager pager = new Pager(blogMapper.getCount(blogTitle), pageNum, pageSize);
        ArrayList<Blog> blog = blogMapper.getBlogByBlogTitle(blogTitle,pager.getStartIndex(), pager.getPageSize());
     HashMap<String,Object> map = new HashMap<>();
     map.put("pager",pager);
     map.put("list",blog);
        return map;

    }

    public ArrayList<Blog> queryBlogByName(String username) {
        // 构建缓存的 key
        String cacheKey = "blog:" + username;

        // 尝试从缓存中获取数据
        ArrayList<Blog> blog = redisService.getCacheObject(cacheKey);

        // 判断缓存中是否有数据
        if (blog != null) {
            System.out.println("1");
            // 缓存命中，直接返回缓存数据
            return blog;

        } else {
            // 缓存未命中，从数据库中查询数据
            blog = blogMapper.getBlogByUserName(username);

            // 将查询结果存入缓存
            redisService.setCacheObject(cacheKey, blog);

            // 返回查询结果
            return blog;
        }
    }
    public String[] uploadpicture(MultipartFile file){
        System.out.println(file);
        return fileFeignClient.upload(file);
    }

    /*
     * 测试sns-point-service模块接口
     */
    public RespResult testAddPoint(Integer userId){
        return pointFeignClient.addBlog(userId);
    }

    /*
     *  测试sns-file-service模块接口
     */
    public String[] testUpload(MultipartFile file){
        System.out.println("testUpload================BlogService================================================="); // test
        return fileFeignClient.upload(file);
    }
}
